package com.example.test_demo.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.sql.Connection;
import java.sql.SQLException;

public class ConnectionConfig {

    public HikariDataSource config(){
        HikariDataSource config = new HikariDataSource();
        config.setDriverClassName("com.mysql.cj.jdbc.Driver");
        config.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        config.setUsername("root");
        config.setPassword("20001204yuan");
        config.setMaximumPoolSize(5);
        config.setMinimumIdle(2);
        return config;
    }
    @Bean
    public HikariDataSource dataSource(){
        HikariDataSource ds = new HikariDataSource(config());
        return ds;
    }

    public static void main(String[] args) throws SQLException {

        AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(JavaConfig.class);
        Connection connection = ctx.getBean(HikariDataSource.class).getConnection();

        ctx.registerShutdownHook();//销毁

    }

}
